package com.nd.android.u.ui.chatBottomFunction;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;

import com.nd.android.u.allCommonUtils.SdCardUtils;
import com.nd.android.u.allCommonUtils.ToastUtils;
import com.nd.android.u.controller.ChatConst.LocalMessageType;
import com.nd.android.u.controller.ChatGlobalVariable;
import com.nd.android.u.controller.factory.ChatBottomFunctionFactory.ISpecialProccess;
import com.nd.android.u.controller.innerInterface.IBottomFunction;
import com.nd.android.u.ui.R;
import com.nd.android.u.ui.widge.chatfragment.BottomFragment;
import com.product.android.business.ApplicationVariable;
import com.product.android.utils.FileHelper;

/**
 * 打开相机
 * @author yufeng
 * @version 创建时间：2014-7-18 上午10:40:44
 */
public class BottomFunction_Camera implements IBottomFunction {

    @Override
    public String getLabel() {
        return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.list_grid_item_opencapture);
    }

    @Override
    public int getAppResId() {
    	return R.drawable.app_capture;
    }

    @Override
    public boolean isEnable(int MessageType, long subType) {
        if(MessageType==LocalMessageType.PUBLIC_NUMBER){
          return false;
        }
        return true;
    }

	@Override
	public void onClick(Activity context, Fragment fragment,ISpecialProccess proccess) {
        if (context == null) {
            if (fragment == null) {
                Log.e("HYK", "The context and fragment are empty!");
                return;
            } else {
                context = fragment.getActivity();
            }
            if(context == null){
                Log.e("HYK", "The context is empty!");
                return;
            }
        }
        if (!SdCardUtils.isSdCardExist()) {
            ToastUtils.display(context, R.string.chat_no_sdcard_photo);
            Log.e("HYK", "Sdcard does not exist!");
            return;
        }
        try {
            File mImageFile = FileHelper.getDefaultImageFile(FileHelper.UPLOAD);
			// 如果存在，则先删除
            if (mImageFile.exists()) {
                if (!mImageFile.delete()) {
                    ToastUtils.display("Delete old image file failed!");
                    Log.e("HYK", "Delete old image file failed!");
                    return;
                }
            }

            // 如果都无法创建这个文件，则直接返回
            if ( !mImageFile.createNewFile()) {
                ToastUtils.display("Unable to create the image files!");
                Log.e("HYK", "Unable to create the image files!");
                return;
			} else {
				// 如果成功创建则先删除
				if(!mImageFile.delete()){
                    ToastUtils.display("Unable to delete old image file failed!");
                    Log.e("HYK", "Unable to delete old image file failed!");
                    return;
                }
			}
			
            Uri mImageUri = Uri.fromFile(mImageFile);
//            Uri mImageUri = Uri.parse(mImageFile.getPath());
            if (mImageUri == null || TextUtils.isEmpty(mImageUri.toString())) {
                ToastUtils.display("The image file URI is empty!");
                Log.e("HYK", "The image file URI is empty!");
                return;
            }
            
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
            if(fragment!=null){
                fragment.startActivityForResult(intent, BottomFragment.REQUEST_IMAGE_CAPTURE);
            }else{
                context.startActivityForResult(intent, BottomFragment.REQUEST_IMAGE_CAPTURE);
            }
            ChatGlobalVariable.getInstance().isOpenCamera = true;
            if (proccess!=null){
            	proccess.specialProccess(mImageUri);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
